﻿using System;
using System.Web.Routing;
using System.Runtime.CompilerServices;

namespace BlogEngine.NET.MVC.Core.Helpers
{
    public static class RouteHelper
    {
        /// <summary>
        /// Code from http://blog.codeville.net/2008/11/05/app-areas-in-aspnet-mvc-take-2/
        /// </summary>
        public static void CreateArea( this RouteCollection routes, string areaName, string controllersNamespace, params Route[] routeEntries )
        {
            foreach ( var route in routeEntries )
            {
                if ( route.Constraints == null )
                    route.Constraints = new RouteValueDictionary();
                if ( route.Defaults == null )
                    route.Defaults = new RouteValueDictionary();
                if ( route.DataTokens == null )
                    route.DataTokens = new RouteValueDictionary();

                route.Constraints.Add( "area", areaName );
                route.Defaults.Add( "area", areaName );
                route.DataTokens.Add( "namespaces", new string[] { controllersNamespace } );

                if ( !routes.Contains( route ) ) // To support "new Route()" in addition to "routes.MapRoute()"
                    routes.Add( route );
            }
        }
    }
}
